/*
 * @Author: xianfuyou
 * @Date: 2022-04-01 11:15:53
 * @LastEditors: your name
 * @LastEditTime: 2022-04-11 13:11:27
 * @Description: file content
 */
import React, { useEffect, useState } from 'react'
import { Modal, Form, Input, Row, Col, message } from 'antd'
import './css/Model.css'
import Axios from '../../utils/myAxios'
import Qs from 'qs'
import PubSub from 'pubsub-js';

import store from '../../redux/store'
import { getOrderListsAction,getAllPriceAction } from '../../redux/action/orderAction/orderAction'


const { TextArea } = Input

export default function DelModel(props) {
  // const baseUrl = 'http://139.155.237.161:8080'
  const { isShow, pHandleOk, pHandleCancel, orderId, requestJson,getAllPrice } = props
  const [form] = Form.useForm()

  const [fn,setFn] = useState(null)

  const handleOk = () => {
    // console.log(form.getFieldValue('reason'))
    delOrder(orderId, form.getFieldValue('reason'))
  };

  const handleCancel = () => {
    pHandleCancel(false);
  };

  // 删除一个订单记录
  const delOrder = (id, deleteCause) => {
    console.log(id, deleteCause)
    Axios.post('/order/deleteOrderById', Qs.stringify({ id: id, deleteCause: deleteCause }),
      { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(res => {
        console.log(res)
        if (res.data.code === 200) {
          message.success('删除成功')
          store.dispatch(getOrderListsAction(requestJson))
          store.dispatch(getAllPriceAction())
        } else {
          message.error('删除失败')
        }
        pHandleOk(false);
      })
  }

  return (
    <div>
      <Modal
        className="xfy_dep_add_modal del_order"
        title="删除原因"
        okText="保存"
        cancelText="关闭"
        onOk={handleOk}
        onCancel={handleCancel}
        afterClose={() => { form.resetFields() }}
        visible={isShow}
        destroyOnClose={true}
        forceRender={true}
      >
        <Form preserve={false} form={form}>
          <Row justify={'center'}>
            <Col className={'title'}>
              填写订单删除原因
            </Col>
            <Col span={24}>
              <Form.Item name="reason">
                <TextArea rows={4}>
                </TextArea>
              </Form.Item>
            </Col>
            <Col span={24} className={'hint'}>
              提示：将同时删除订单商品记录和对应财务记录，对应订单商品将会退回库存数量
            </Col>
          </Row>
        </Form>
      </Modal>
    </div>
  )
}
